<!--
 * @Description:
 * @version:
 * @Author: 尹鹏孝
 * @Date: 2023-03-23 11:34:29
 * @LastEditors: 尹鹏孝
 * @LastEditTime: 2023-03-23 16:48:04
-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Proxy代理器</title>
</head>
<body>
    <script>
        // Proxy代理
     let obj =   new Proxy({},{
            get:function(target,propKey,receiver){
                console.log(`getting:${propKey}`)
                return Reflect.get(target,propKey,receiver)
            },
            set(target,propKey,value,receiver){
                console.log(`setting:${propKey}`)
                return Reflect.set(target,propKey,value,receiver)

            }
        })
        obj.count =1;
        ++obj.count;
        // console.log(obj)

        //链式操作
    //    let ms=function(){};
    //    ms.prototype={
    //     value:0,
    //     add:function(value){
    //         this.value=value+this.value;
    //         return this;
    //     },

    //    }
    //    let ds = new ms()
    //  console.log(ds.add(1).add(2).add(2).add(2).add(2).add(2).add(2).add(2).add(2))
    let mg={
        add:function(val){
            this.value=this.value+val;
            return this;
        },
        value:0,
    }
   console.log(mg.add(2).add(3))

//    var proxyVue = new Proxy(target,handle)

 const myProxy =  Object.defineProperties({},{
    foo:{
        value:123,
        writeable:false,
        configurable:false,
        enumerable:false
    }
 });

 const handler={
    get(target,propKey){
        return 'abc';
    }
 }
 const proxy = new Proxy(myProxy,handler);
try {
     proxy.foo;
} catch (error) {

}
//proxy写的的一个链式操作=>。
var pipe = function(value){
    var funcStack=[];
    var operxy = new Proxy({name:123},{
        get:function(pipeObject,fnName,res){
            console.log(pipeObject)
            console.log(res)
            if(fnName==='get'){
                return funcStack.reduce(function(val,fn){
                    return fn(val)
                },value)
            }
            funcStack.push(window[fnName])
            return operxy;
        }
    })
    return operxy
}

var double =n=>n*2;
var pow = n=>n*n;
var parseInt = n=>n.toString().split("").reverse().join("")|0;
let m = pipe(3).double.pow.parseInt.get;
    console.log(m);
    </script>
</body>
</html>